<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>PyMOTW: optparse &mdash; PyMOTW v1.2 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '1.2',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="author" title="关于这些文档" href="../about.html" />
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="PyMOTW v1.2 documentation" href="../index.html" />
    <link rel="next" title="PyMOTW: shutil" href="shutil.html" />
    <link rel="prev" title="PyMOTW: shelve" href="shelve.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="一般索引"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="shutil.html" title="PyMOTW: shutil"
             accesskey="N">后一篇</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="PyMOTW: shelve"
             accesskey="P">前一篇</a> |</li>
        <li><a href="../index.html">PyMOTW v1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  
  <div class="section" id="pymotw-optparse">
<h1>PyMOTW: optparse<a class="headerlink" href="#pymotw-optparse" title="Permalink to this headline">¶</a></h1>
<ul class="simple">
<li>模块：optparse</li>
<li>目的：命令行参数解析, 可以取代getopt</li>
<li>python版本：2.3</li>
</ul>
<div class="section" id="id1">
<h2>描述<a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h2>
<p>optparse是一个当前可选的命令行解析模块, 它提供了一些在getopt中不含有的特性, 如type conversion(类型转换), option callbacks(参数回调)以及automatice help generation(自动化帮助生成). 本文没有详细介绍optparse的很多特性, 但它可以帮助你在写命令行程序时能够快速入门.</p>
</div>
<div class="section" id="optionparser">
<h2>创建一个OptionParser<a class="headerlink" href="#optionparser" title="Permalink to this headline">¶</a></h2>
<p>optparser解析参数需要经过2个阶段. 首先, 构建OptionParser实例并配置相关的选项, 然后填入一个参数序列并处理.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">optparse</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
</pre></div>
</div>
<p>通常, 一旦分析器被建立, 每一个选项需要明确的添加到parser中, 并说明当命令行遇到相关的选项时需要如何处理. 在构建OptionParser时也可以传入一个选项列表, 但这种形式不经常使用.</p>
</div>
<div class="section" id="id2">
<h2>定义选项<a class="headerlink" href="#id2" title="Permalink to this headline">¶</a></h2>
<p>利用add_option()方法可以每次增加一个选项. 在参数列表的开始, 任何未命名的字符串参数都将被视为选项名. 如果要为一个选项创建别名, 比如为同一个选项增加一个短的或长的命名, 那么简单传递同名字符串即可.</p>
<p>不同于getopt, 只能分析选项, optparse是一个完整的选项分析库, Option(选项)可以被不同的方法处理, 通过在add_option()方法中指定action(行为)参数. 支持的行为包括存储参数(单独或作为列表的一部分), 当一个选项出现时(包括对布尔开关true/false的特殊处理)存储其常量值, 计算一个选项出现的次数以及调用一个callback(回调函数).</p>
<p>默认的行为是存储这个选项的参数. 如果给定了type(类型), 那么在存储前, 这个参数值将被转化成这个类型. 如果给定了dest(目标参数), 那么当命令行参数被解析时, 选项值被存储在该选项对象的dest中.</p>
</div>
<div class="section" id="id3">
<h2>分析一个命令行<a class="headerlink" href="#id3" title="Permalink to this headline">¶</a></h2>
<p>一旦所有的选项被定义好, 命令行被作为一个参数字符串传递给parse_args()方法. 一般, 参数可以从sys.argv[1:]中得到, 当然你可以传递自己的列表. 选项处理时使用GNU/POSIX语法, 因此, 选项和参数值可以在参数序列中混合使用.</p>
<p>从parse_args()方法返回的是一个二维元组, 包含一个optparse Values实例和在命令行中未被解析的参数列表. Values实例将选项值作为属性, 如果你定义了一个选项的dest为&#8221;myoption&#8221;, 可以通过option.myoption访问该选项的值.</p>
</div>
<div class="section" id="id4">
<h2>简单示例<a class="headerlink" href="#id4" title="Permalink to this headline">¶</a></h2>
<p>如下一个简单例子有三个不同的选项, 一个布尔选项(-a), 一个字符串选项(-b)和一个整型选项(-c).</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">optparse</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-a&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-b&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;b&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-c&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;c&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;int&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">&#39;-a&#39;</span><span class="p">,</span> <span class="s">&#39;-bval&#39;</span><span class="p">,</span> <span class="s">&#39;-c&#39;</span><span class="p">,</span> <span class="s">&#39;3&#39;</span><span class="p">])</span>
</pre></div>
</div>
<p>命令行中选项解析的规则和getopt.gnu_getopt()一样, 因此有两种方法传递单字符串选项的值, 上述示例使用了两种方法-bval和-c val.</p>
<div class="highlight-python"><pre>$ python optparse_short.py
(&lt;Values at 0xe29b8: {'a': True, 'c': 3, 'b': 'val'}&gt;, [])</pre>
</div>
<p>注意, c所关联的值的类型是整型, OptionParser在存储之前会转换成指定类型. 不同于getopt, optparse处理长选项名时和短选项名是没有任何区别的.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--noarg&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--witharg&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;witharg&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--witharg2&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="n">dest</span><span class="o">=</span><span class="s">&quot;witharg2&quot;</span><span class="p">,</span> <span class="nb">type</span><span class="o">=</span><span class="s">&quot;int&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span> <span class="s">&#39;--noarg&#39;</span><span class="p">,</span> <span class="s">&#39;--witharg&#39;</span><span class="p">,</span> <span class="s">&#39;val&#39;</span><span class="p">,</span> <span class="s">&#39;--witharg2=3&#39;</span> <span class="p">])</span>
</pre></div>
</div>
<p>结果相同的：</p>
<div class="highlight-python"><pre>$ python optparse_long.py
(&lt;Values at 0xd3ad0: {'noarg': True, 'witharg': 'val', 'witharg2': 3}&gt;, [])</pre>
</div>
</div>
<div class="section" id="getopt">
<h2>与getopt的比较<a class="headerlink" href="#getopt" title="Permalink to this headline">¶</a></h2>
<p>如下实现一个与getopt之前示例相同功能的optparse例子:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">optparse</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="k">print</span> <span class="s">&#39;ARGV      :&#39;</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">argv</span><span class="p">[</span><span class="mf">1</span><span class="p">:]</span>
<span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-o&#39;</span><span class="p">,</span> <span class="s">&#39;--output&#39;</span><span class="p">,</span>
            <span class="n">dest</span><span class="o">=</span><span class="s">&quot;output_filename&quot;</span><span class="p">,</span>
            <span class="n">default</span><span class="o">=</span><span class="s">&quot;default.out&quot;</span><span class="p">,</span>
            <span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;-v&#39;</span><span class="p">,</span> <span class="s">&#39;--verbose&#39;</span><span class="p">,</span>
            <span class="n">dest</span><span class="o">=</span><span class="s">&quot;verbose&quot;</span><span class="p">,</span>
            <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
            <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span>
            <span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--version&#39;</span><span class="p">,</span>
            <span class="n">dest</span><span class="o">=</span><span class="s">&quot;version&quot;</span><span class="p">,</span>
            <span class="n">default</span><span class="o">=</span><span class="mf">1.0</span><span class="p">,</span>
            <span class="nb">type</span><span class="o">=</span><span class="s">&quot;float&quot;</span><span class="p">,</span>
            <span class="p">)</span>
<span class="n">options</span><span class="p">,</span> <span class="n">remainder</span> <span class="o">=</span> <span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
<span class="k">print</span> <span class="s">&#39;VERSION   :&#39;</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">version</span>
<span class="k">print</span> <span class="s">&#39;VERBOSE   :&#39;</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">verbose</span>
<span class="k">print</span> <span class="s">&#39;OUTPUT    :&#39;</span><span class="p">,</span> <span class="n">options</span><span class="o">.</span><span class="n">output_filename</span>
<span class="k">print</span> <span class="s">&#39;REMAINING :&#39;</span><span class="p">,</span> <span class="n">remainder</span>
</pre></div>
</div>
<p>注意, -o和&#8211;output选项是如何在同一时刻被定义的, 命令行中可以使用任何一种选项.</p>
<div class="highlight-python"><pre>$ python optparse_getoptcomparison.py -o output.txt
ARGV      : ['-o', 'output.txt']
VERSION   : 1.0
VERBOSE   : False
OUTPUT    : output.txt
REMAINING : []
$ python optparse_getoptcomparison.py --output output.txt
ARGV      : ['--output', 'output.txt']
VERSION   : 1.0
VERBOSE   : False
OUTPUT    : output.txt
REMAINING : []</pre>
</div>
<p>另外, 长选项名的唯一前缀也可以被使用.</p>
<div class="highlight-python"><pre>$ python optparse_getoptcomparison.py --out output.txt
ARGV      : ['--out', 'output.txt']
VERSION   : 1.0
VERBOSE   : False
OUTPUT    : output.txt
REMAINING : []</pre>
</div>
</div>
<div class="section" id="option-callbacks">
<h2>Option Callbacks(选项回调)<a class="headerlink" href="#option-callbacks" title="Permalink to this headline">¶</a></h2>
<p>除了直接为选项存储参数, 另一种选择是定义callback function, 当命令行中出现该选项时调用, 选项的callbacks有4个参数, 分别是引起callback的optparse.Option实例, 命令行中的选项字符串, 选项关联的参数值以及处理解析工作的optparse.OptionParser实例.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">optparse</span>

<span class="k">def</span> <span class="nf">flag_callback</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;flag_callback:&#39;</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">option:&#39;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">option</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">opt_str:&#39;</span><span class="p">,</span> <span class="n">opt_str</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">value:&#39;</span><span class="p">,</span> <span class="n">value</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">parser:&#39;</span><span class="p">,</span> <span class="n">parser</span>
    <span class="k">return</span>

<span class="k">def</span> <span class="nf">with_callback</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;with_callback:&#39;</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">option:&#39;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">option</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">opt_str:&#39;</span><span class="p">,</span> <span class="n">opt_str</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">value:&#39;</span><span class="p">,</span> <span class="n">value</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">parser:&#39;</span><span class="p">,</span> <span class="n">parser</span>
    <span class="k">return</span>

<span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--flag&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span> <span class="n">callback</span><span class="o">=</span><span class="n">flag_callback</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--with&#39;</span><span class="p">,</span>
    <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span>
    <span class="n">callback</span><span class="o">=</span><span class="n">with_callback</span><span class="p">,</span>
    <span class="nb">type</span><span class="o">=</span><span class="s">&quot;string&quot;</span><span class="p">,</span>
    <span class="n">help</span><span class="o">=</span><span class="s">&quot;Include optional feature&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">&#39;--with&#39;</span><span class="p">,</span> <span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="s">&#39;--flag&#39;</span><span class="p">])</span>
</pre></div>
</div>
<p>在这个例子中, &#8211;with选项被配置成处理字符串参数(当然, 其他类型也是同样支持的).</p>
<div class="highlight-python"><pre>$ python optparse_callback.py
with_callback:
     option: &lt;Option at 0x78b98: --with&gt;
     opt_str: --with
     value: foo
     parser: &lt;optparse.OptionParser instance at 0x78b48&gt;
flag_callback:
     option: &lt;Option at 0x7c620: --flag&gt;
     opt_str: --flag
     value: None
     parser: &lt;optparse.OptionParser instance at 0x78b48&gt;</pre>
</div>
</div>
<div class="section" id="id5">
<h2>帮助信息<a class="headerlink" href="#id5" title="Permalink to this headline">¶</a></h2>
<p>OptionParser自动为每个选项集合包含一个help选项, 因此, 用户在运行程序时在命令行输入&#8211;help来看介绍, 帮助信息为所有选项指示它们是否需要传入一个参数, 也可以通过在add_option()中定义帮助文本来为一个选项定义更多的描述.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--no-foo&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store_true&quot;</span><span class="p">,</span>
    <span class="n">default</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span>
    <span class="n">dest</span><span class="o">=</span><span class="s">&quot;foo&quot;</span><span class="p">,</span>
    <span class="n">help</span><span class="o">=</span><span class="s">&quot;Turn off foo&quot;</span><span class="p">,</span>
<span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--with&#39;</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">&quot;store&quot;</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">&quot;Include optional feature&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">()</span>
</pre></div>
</div>
<p>选项按字母顺序显示, 别名显示在同一行, 当选项带有一个参数时, dest值将作为参数名字出现在help输出中, 帮助信息将出现在这列中.</p>
<div class="highlight-python"><pre>$ python optparse_help.py --help
Usage: optparse_help.py [options]

Options:
-h, --help   show this help message and exit
--no-foo     Turn off foo
--with=WITH  Include optional feature</pre>
</div>
<p>利用nargs选项可以配置callbacks接收多个参数.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">with_callback</span><span class="p">(</span><span class="n">option</span><span class="p">,</span> <span class="n">opt_str</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">parser</span><span class="p">):</span>
    <span class="k">print</span> <span class="s">&#39;with_callback:&#39;</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">option:&#39;</span><span class="p">,</span> <span class="nb">repr</span><span class="p">(</span><span class="n">option</span><span class="p">)</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">opt_str:&#39;</span><span class="p">,</span> <span class="n">opt_str</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">value:&#39;</span><span class="p">,</span> <span class="n">value</span>
    <span class="k">print</span> <span class="s">&#39;</span><span class="se">\t</span><span class="s">parser:&#39;</span><span class="p">,</span> <span class="n">parser</span>
    <span class="k">return</span>

<span class="n">parser</span> <span class="o">=</span> <span class="n">optparse</span><span class="o">.</span><span class="n">OptionParser</span><span class="p">()</span>
<span class="n">parser</span><span class="o">.</span><span class="n">add_option</span><span class="p">(</span><span class="s">&#39;--with&#39;</span><span class="p">,</span>
    <span class="n">action</span><span class="o">=</span><span class="s">&quot;callback&quot;</span><span class="p">,</span>
    <span class="n">callback</span><span class="o">=</span><span class="n">with_callback</span><span class="p">,</span>
    <span class="nb">type</span><span class="o">=</span><span class="s">&quot;string&quot;</span><span class="p">,</span>
    <span class="n">nargs</span><span class="o">=</span><span class="mf">2</span><span class="p">,</span>
    <span class="n">help</span><span class="o">=</span><span class="s">&quot;Include optional feature&quot;</span><span class="p">)</span>
<span class="n">parser</span><span class="o">.</span><span class="n">parse_args</span><span class="p">([</span><span class="s">&#39;--with&#39;</span><span class="p">,</span> <span class="s">&#39;foo&#39;</span><span class="p">,</span> <span class="s">&#39;bar&#39;</span><span class="p">])</span>
</pre></div>
</div>
<p>在这个例子中, 参数作为一个元组传递给callback function的value参数.</p>
<div class="highlight-python"><pre>$ python optparse_callback_nargs.py
with_callback:
     option: &lt;Option at 0x7c4e0: --with&gt;
     opt_str: --with
     value: ('foo', 'bar')
     parser: &lt;optparse.OptionParser instance at 0x78a08&gt;</pre>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="../index.html">目录</a></h3>
            <ul>
<li><a class="reference external" href="">PyMOTW: optparse</a><ul>
<li><a class="reference external" href="#id1">描述</a></li>
<li><a class="reference external" href="#optionparser">创建一个OptionParser</a></li>
<li><a class="reference external" href="#id2">定义选项</a></li>
<li><a class="reference external" href="#id3">分析一个命令行</a></li>
<li><a class="reference external" href="#id4">简单示例</a></li>
<li><a class="reference external" href="#getopt">与getopt的比较</a></li>
<li><a class="reference external" href="#option-callbacks">Option Callbacks(选项回调)</a></li>
<li><a class="reference external" href="#id5">帮助信息</a></li>
</ul>
</li>
</ul>

            <h4>前一个主题</h4>
            <p class="topless"><a href="shelve.html" title="前一章节">PyMOTW: shelve</a></p>
            <h4>后一个主题</h4>
            <p class="topless"><a href="shutil.html" title="后一章节">PyMOTW: shutil</a></p>
            <h3>当前主题</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/documents/optparse.txt">显示页面源码</a></li>
            </ul>
          <h3>快速搜索</h3>
            <form class="search" action="../search.html" method="get">
              <input type="text" name="q" size="18" /> <input type="submit" value="提交" />
              <input type="hidden" name="check_keywords" value="yes" />
              <input type="hidden" name="area" value="default" />
            </form>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>导航</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="一般索引"
             accesskey="I">索引</a></li>
        <li class="right" >
          <a href="shutil.html" title="PyMOTW: shutil"
             accesskey="N">后一篇</a> |</li>
        <li class="right" >
          <a href="shelve.html" title="PyMOTW: shelve"
             accesskey="P">前一篇</a> |</li>
        <li><a href="../index.html">PyMOTW v1.2 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; <a href="../copyright.html">Copyright</a> 2008, vbarter &amp; liz.
      使用 <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.5.1创建.
    </div>
  </body>
</html>